﻿#include "OperatorsSectionManager.h"

#include "GUIFrame/PropertyWidget.h"
#include "OperatorsInterface/TreeEventOperator.h"
#include "GUIDialog/GUIPropertyDialog/SectionWidget.h"
#include "GUIDialog/GUIPropertyDialog/SectionInfoDialog.h"
#include "GUIDialog/GUIPropertyDialog/SectionDeleteDialog.h"
#include "GUIDialog/GUICalculateDialog/SelectSetSurfWidget.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"

#include "FITK_Interface/FITKInterfacePhysics/FITKAbstractSection.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaAbstractSection.h"
#include "FITK_Interface/FITKInterfaceFlowOF/FITKOFPhysicsData.h"
namespace ModelOper
{
    OperatorsSectionManager::OperatorsSectionManager()
    {

    }

    OperatorsSectionManager::~OperatorsSectionManager()
    {

    }

    bool OperatorsSectionManager::execGUI()
    {
        QWidget* widget = nullptr;
        QDialog* dialog = nullptr;

        GUI::MainWindow* mainWindow = dynamic_cast<GUI::MainWindow*>(FITKAPP->getGlobalData()->getMainWindow());
        if (mainWindow == nullptr)return false;
        GUI::PropertyWidget* propertyWidget = mainWindow->getPropertyWidget();
        if (propertyWidget == nullptr)return false;

        Interface::FITKOFPhysicsData* physicsData = FITKAPP->getGlobalData()->getPhysicsData<Interface::FITKOFPhysicsData>();
        if (physicsData == nullptr)return false;
        Interface::FITKAbstractSectionManager* sectionManager =physicsData->getSectionManager();
        if (!sectionManager)return false;//获取失败返回nullpttr，因此要检查返回值是否为空，避免空指针

        int objID = -1;
        this->argValue("objID", objID);
        //判断按钮的类型
        switch (_operType)
        {
        case ModelOper::OperManagerBase::Create:
        {
            widget = new GUI::SectionWidget(this,FITKAPP->getGlobalData()->getMainWindow());
            break;
        }
        case ModelOper::OperManagerBase::Edit:
        {
            Interface::FITKAbstractSection* section = sectionManager->getDataByID(objID);
            Interface::FITKAbaAbastractSection* sec = dynamic_cast<Interface::FITKAbaAbastractSection*>(section);
            if (section == nullptr) return false;
            dialog=new GUI::SectionInfoDialog(FITKAPP->getGlobalData()->getMainWindow(),this,sec);
            break;
        }
        case ModelOper::OperManagerBase::Assign:
        {
            Interface::FITKAbstractSection* section = sectionManager->getDataByID(objID);
            Interface::FITKAbaAbastractSection* sec = dynamic_cast<Interface::FITKAbaAbastractSection*>(section);
            if (section == nullptr) return false;
            widget = new GUI::SelectSetSurfaceWidgetBase(this,FITKAPP->getGlobalData()->getMainWindow(),sec);
            break;
        }

        case ModelOper::OperManagerBase::Delete:
        {
            Interface::FITKAbstractSection* section = sectionManager->getDataByID(objID);
            dialog = new GUI::SectionDeleteDialog(section,this);
            break;
        }
        }
        if (mainWindow->getPropertyWidget() && widget) {
            propertyWidget->setWidget(widget);
        }

        if (dialog) {
            dialog->show();
        }
        return false;
    }

    bool OperatorsSectionManager::execProfession()
    {
        // 获取模型树控制器
        auto treeOper = Core::FITKOperatorRepo::getInstance()->getOperatorT<EventOper::TreeEventOperator>("ModelTreeEvent");
        if (treeOper == nullptr) return false;
        treeOper->updateTree();
        return true;
    }
}
